﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Info
    {
        #region Properties
        public List<DetailInfo> Details
        {
            get
            {
                return this.GetDetails();
            }
        }
        #endregion

        #region Methods
        public DetailInfo GetDetail(int lang)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailInfos.Where(p => p.ID_Info == this.ID && p.ID_Language == lang);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<DetailInfo> GetDetails()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.DetailInfos.Where(p => p.ID_Info == this.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }
        #endregion

        #region Static Methods
        public static Info Parse(System.Object value)
        {
            Info temp = null;
            try
            {
                temp = (Info)value;
            }
            catch (Exception ex)
            {
            }
            return temp;
        }

        public static List<Info> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Infos.OrderByDescending(p => p.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Info> GetListAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Infos.Where(p => p.ID_Status != 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Info> GetListNotAvailable()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Infos.Where(p => p.ID_Status == 2);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        private static List<ErrorItem> InputInfomationValidation(Info info)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (info.Tel == "")
                errorList.Add(new ErrorItem("Tel", "Tel is required."));
            if (info.Tel.Length > 20)
                errorList.Add(new ErrorItem("Tel", "Tel is only allow up to 20 characters."));
            if (info.Mobile == "")
                errorList.Add(new ErrorItem("Mobile", "Mobile is required."));
            if (info.Mobile.Length > 20)
                errorList.Add(new ErrorItem("Mobile", "Mobile is only allow up to 20 characters."));
            if (info.Logo == null)
                errorList.Add(new ErrorItem("Logo", "Logo is required."));
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Info ic, int id_User, int id_Lang, int isChangeLogo)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Info oi = _db.Infos.Single(p => p.ID == ic.ID);
                String content = "";
                content = oi.Tel + "_" + oi.Mobile + "_" + oi.ID_Status;
                int errorMess = _db.ChangeInfo(id_User, ic.ID, ic.Tel, ic.Mobile, ic.ID_Status, ic.Logo, oi.Logo, content, 0, 0, isChangeLogo);
                
                    if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }

        public static List<ErrorItem> ChangeInformation(Info ic, int id_User, int id_Log, int id_Lang, int isChangeLogo)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            List<ErrorItem> errorList = InputInfomationValidation(ic);
            if (errorList.Count == 0)
            {
                Info oi = _db.Infos.Single(p => p.ID == ic.ID);
                int errorMess = _db.ChangeInfo(id_User, ic.ID, ic.Tel, ic.Mobile, ic.ID_Status, ic.Logo, oi.Logo, "", id_Log, 1, isChangeLogo);

                if(errorMess != 1) errorList.Add(new ErrorItem("ErrorMessage", _db.ErrorMessages.Single(p => p.ID_ErrorMessage == errorMess && p.ID_Language == id_Lang).Description));
            }
            return errorList;
        }
        #endregion

        #region Search

        private static String BuildQuery(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList)
        {
            String query = "";

            DateTime noValue = new DateTime(2000, 1, 1);

            foreach (KeyValuePair<String, System.Object> kvp in parametersList)
            {
                if (query.Length != 0)
                    query += " AND ";
                if (kvp.Key == "ID" || kvp.Key == "ID_Language")
                {
                    if (int.Parse(kvp.Value.ToString()) >= 1)
                        query += kvp.Key + " = " + int.Parse(kvp.Value.ToString());
                }
                else if (kvp.Key == "CompanyName" || kvp.Key == "Description" || kvp.Key == "Address" || kvp.Key == "Tel" || kvp.Key == "Mobile")
                {
                    if (exactList[kvp.Key])
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " = '" + kvp.Value.ToString() + "'";
                    }
                    else
                    {
                        if (kvp.Value.ToString() != "")
                            query += kvp.Key + " LIKE '%" + kvp.Value.ToString() + "%'";
                    }
                }
                else if (kvp.Key == "CreatedTime")
                {
                    if (DateTime.Parse(kvp.Value.ToString()) != noValue)
                        query += "CreatedTime" + " = " + DateTime.Parse(kvp.Value.ToString()).ToString("yyyy-MM-dd") + "";
                }
            }

            if (parametersList.ContainsKey("CreatedbeginDate") && parametersList.ContainsKey("CreatedendDate"))
            {
                query += " DATEDIFF(day, " + DateTime.Parse(parametersList["CreatedendDate"].ToString()).ToString("yyyy-MM-dd") + " , CreatedTime) >= 0";
                query += " DATEDIFF(day, CreatedTime , " + DateTime.Parse(parametersList["CreatedbeginDate"].ToString()).ToString("yyyy-MM-dd") + ") >= 0";
            }

            return query;
        }

        public static System.Object GetInfo(Dictionary<String, System.Object> parametersList, Dictionary<String, bool> exactList, bool oneRow)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            String query = BuildQuery(parametersList, exactList);
            var v = _db.ExecuteQuery<Info>(query);
            if (oneRow)
            {
                if (v.Count() == 0)
                    return null;
                return v.First();
            }
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        #endregion
    }
}